﻿using System;
using System.Collections.Generic;
using FinPlusComponents;
using FinPlusCommon;

namespace FinPlusAnalytics
{
    public sealed class Markets : FinPlusComponent
    {
        private static Markets _instance;
        private static object _lock = new Object();
        private static Dictionary<string, Market> _markets = new Dictionary<string, Market>();
        
        //construct
        public static Markets Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                            _instance = new Markets();
                    }
                }

                return _instance;
            }
        }

        //public
        public Market MarketSet(string name, DateTime todaysDate)
        {
            Market market = null;
            lock (_lock)
            {
                if (!_markets.ContainsKey(name))
                    _markets[name] = market = new Market(name, todaysDate);
                else
                    market = _markets[name];
            }

            return market;
        }

        public Market GetMarket(string name)
        {
            if (_markets.ContainsKey(name))
                return _markets[name];
            else
                throw new AnalyticsException(string.Format("cant find {0} market", name));
        }
    }
}
